package com.groupbuying.store.controller.forInternal;

import com.groupbuying.store.context.FilterContextHandler;
import com.groupbuying.store.utils.PageUtils;
import com.groupbuying.store.utils.ParamUtils;
import com.groupbuying.store.utils.Query;
import com.groupbuying.store.utils.R;
import com.groupbuying.store.domain.forManage.StoreRoleDO;
import com.groupbuying.store.service.forManage.StoreRoleService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;

/**
 * @author groupbuying
 * 角色管理
 */
@Api("服务商角色管理")
@RequestMapping("/role")
@RestController
public class StoreRoleController {
    @Autowired
    StoreRoleService roleService;

    @GetMapping()
    PageUtils list(@RequestParam Map<String, Object> params) {
        // 加入服务商id
        String providerId = FilterContextHandler.getName();
        params.put("providerId",providerId);
        Query query = new Query(params);
        List<StoreRoleDO> roleDOS = roleService.listLike(query);
        int total = roleService.countLike(query);
        PageUtils pageUtil = new PageUtils(roleDOS, total);
        return pageUtil;
    }

    @GetMapping("/userId/{userId}")
    List<BigInteger> roleIdByUserId(@PathVariable String userId){
        return roleService.roleIdsByUserId(userId);
    }

    @PostMapping
    R save(@RequestBody StoreRoleDO roleDO){
        return roleService.internal_saveStoreRole(roleDO);
    }

    @PutMapping
    R update(@RequestBody StoreRoleDO roleDO){
        if(roleService.update(roleDO)>0){
            return R.ok();
        }
        return R.error();
    }

    /**
     * 为全体总经理统一增加权限
     * */
    @PostMapping("/addMenuForAdmin")
    public R addMenuForAdmin(HttpServletRequest request){
        Map<String, Object> mapParams = ParamUtils.getParamMap(request);
        return roleService.addMenuForAdmin(mapParams);
    }
}
